package com.yvon.service.demo.gateway.core.session.defaults;

import com.yvon.service.demo.gateway.core.datasource.DataSource;
import com.yvon.service.demo.gateway.core.datasource.DataSourceFactory;
import com.yvon.service.demo.gateway.core.datasource.unpooled.UnpooledDataSourceFactory;
import com.yvon.service.demo.gateway.core.session.Configuration;
import com.yvon.service.demo.gateway.core.session.GatewaySession;
import com.yvon.service.demo.gateway.core.session.GatewaySessionFactory;

/**
 * 默认网关会话工厂
 * 在网关会话中需要对数据源进行创建使用，在DefaultGatewaySessionFactory工厂中构建数据源，在DefaultGatewaySession使用数据源
 *
 * @author yvon
 * @since 2023-03-20
 */
public class DefaultGatewaySessionFactory implements GatewaySessionFactory {

    private final Configuration configuration;

    public DefaultGatewaySessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public GatewaySession openSession(String uri) {
        // 获取数据源连接信息：这里吧 Dubbo、HTTP抽象为一种连接资源
        DataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
        dataSourceFactory.setProperties(configuration, uri);
        DataSource dataSource = dataSourceFactory.getDataSource();
        return new DefaultGatewaySession(configuration, uri, dataSource);
    }

}